package com.lhgray.sglhome.presentation.viewModel

import android.net.Uri
import androidx.lifecycle.viewModelScope
import com.freightoa.foundation.app.MMKVUtils
import com.freightoa.foundation.app.MMKVkEY
import com.freightoa.foundation.viewmodel.BaseState
import com.freightoa.foundation.viewmodel.BaseViewModel
import com.freightoa.foundation.viewmodel.UiIntent
import com.lhgray.sglhome.domain.usecase.BarCodeUseCase
import com.lhgray.sglhome.domain.usecase.HomeUseCase
import com.lhgray.sglhome.domain.usecase.OrderListUseCase
import com.lhgray.sglhome.domain.model.SourceData
import com.lhgray.sglhome.domain.model.UserData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject


sealed interface HomeState : BaseState {
    data object Idle : HomeState
    data class Loading(val isLoading: Boolean? = true) : HomeState
    data class Success(val message: String?) : HomeState
    data class Toast(val message: String?) : HomeState
    data class Error(val errorMessage: String?, val index: Int) : HomeState
    data object NavigateToLogin : HomeState  // 新增跳转状态
}

sealed interface HomeIntent : UiIntent {
    data class Home(val list: MutableList<Uri>) : HomeIntent
    data object Idle : HomeIntent
    data object Logout : HomeIntent
}

@HiltViewModel
class HomeViewModel @Inject constructor(
    private val homeUseCase: HomeUseCase,
    private val orderListUseCase: OrderListUseCase,
    private val barCodeUseCase: BarCodeUseCase,
) :
    BaseViewModel<HomeState, HomeIntent>() {

    private val _uiStateFlow = MutableStateFlow<HomeState>(HomeState.Idle)
    private val _uiStateOrderList = MutableStateFlow(SourceData())
    private val _fileNo = MutableStateFlow("")
    private val _userData = MutableStateFlow(
        UserData(userName = MMKVUtils.getString(MMKVkEY.userName, ""))
    )
    val userData: StateFlow<UserData> = _userData.asStateFlow()
    var fileNo: StateFlow<String> = _fileNo.asStateFlow()
    override val uiState: StateFlow<HomeState> = _uiStateFlow.asStateFlow()

    override fun handleIntent(intent: HomeIntent) {
        when (intent) {
            is HomeIntent.Home -> {

            }
            is HomeIntent.Idle -> {
                _uiStateFlow.value = HomeState.Idle
            }
            is HomeIntent.Logout->{
                MMKVUtils.clearAll()
                _uiStateFlow.value = HomeState.NavigateToLogin  // 触发跳转状态
            }
        }

    }

    fun upFileNo(fileNo: String) {
        _fileNo.value = fileNo
    }

    private var index: Int = 0
    fun uploadError(errorMessage: String) {
        if (errorMessage.isNotEmpty()) {
            index++
            _uiStateFlow.value = HomeState.Error(errorMessage, index)
        }
    }


    fun uploadLoadingOrError(isShowLoading: Boolean, errorMessage: String,toast:String?="") {
        if (errorMessage.isNotEmpty()) {
            index++
            _uiStateFlow.value = HomeState.Error(errorMessage, index)
            return
        }
        if (isShowLoading) {
            _uiStateFlow.value = HomeState.Loading(true)
            return
        }
        if (toast?.isNotEmpty() == true){
            _uiStateFlow.value = HomeState.Toast(toast)
            return
        }
        _uiStateFlow.value = HomeState.Idle
    }

    fun upBarcode(barcode: String) {
        if (barcode.isEmpty()) return
        _uiStateFlow.value = HomeState.Loading(true)
        viewModelScope.launch {
            val fileNo = barCodeUseCase.invoke(barcode)
            _fileNo.value = fileNo.getOrDefault("")
            _uiStateFlow.value = HomeState.Idle
        }
    }
}